package com.tsd.common.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tsd.common.entity.CommonParameterValue;
import com.tsd.common.entity.CommonParameterValueExt;
import com.tsd.common.service.CommonParameterValueService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.controller.SystemBaseController;
import com.tsd.system.dao.SqlSelectMapper;
import com.tsd.system.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：CommonParameterValuecommon_parameter_value 控制层
 *
 * @author zsy
 * @date 2019/12/06 10:58:19
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "common_parameter_value-common_parameter_value", description = "common_parameter_value的API描述")
public class CommonParameterValueController extends SystemBaseController {

    @Resource
    private CommonParameterValueService commonParameterValueService;
    @Resource
    private SqlSelectMapper sqlSelectMapper;

    @ApiOperation(value = "根据id获取common_parameter_value", notes = "根据id获取common_parameter_value")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "common_parameter_valueid", required = true, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getCommonParameterValueById.action", method = RequestMethod.POST)
    public HttpResult<CommonParameterValue> getCommonParameterValueById() {
        HttpResult<CommonParameterValue> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            CommonParameterValue commonParameterValue = commonParameterValueService.getCommonParameterValueById(id);
            hr.setData(commonParameterValue);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据sid获取common_parameter_value", notes = "根据id获取common_parameter_value")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "sid", required = true, dataType = "String", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getCommonParameterValueBySid.action", method = RequestMethod.POST)
    public HttpResult<CommonParameterValue> getCommonParameterValueBySid() {
        HttpResult<CommonParameterValue> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            String sid = HlpUtils.getString(map, "sid");
            super.checkEmpty(sid, "请提供参数sid");
            CommonParameterValue commonParameterValue = commonParameterValueService.getCommonParameterValueBySid(sid);
            hr.setData(commonParameterValue);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除common_parameter_value", notes = "根据id删除common_parameter_value")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "common_parameter_valueid", required = true, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteCommonParameterValue.action", method = RequestMethod.POST)
    public HttpResult<String> deleteCommonParameterValue() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            commonParameterValueService.deleteCommonParameterValue(id, null);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据sid删除common_parameter_value", notes = "根据sid删除common_parameter_value")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "common_parameter_valuesid", required = true, dataType = "String", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteCommonParameterValues.action", method = RequestMethod.POST)
    public HttpResult<String> deleteCommonParameterValues() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            SysUser opUser = super.getSysUser();
            Object sids = map.get("sids");
            super.checkEmpty(sids, "请提供参数sids");
            List<String> list = JSON.parseArray(JSON.toJSONString(sids), String.class);
            commonParameterValueService.deleteCommonParameterValues(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存common_parameter_value", notes = "保存common_parameter_value，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "common_parameter_value", required = true, paramType = "body", dataType = "CommonParameterValue")
    })
    @ResponseBody
    @RequestMapping(value = "/saveCommonParameterValue.action", method = RequestMethod.POST)
    public HttpResult<String> saveCommonParameterValue() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            CommonParameterValueExt valueExt = super.getParams(request, CommonParameterValueExt.class);
            commonParameterValueService.saveCommonParameterValue(valueExt, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询CommonParameterValue", notes = "说明")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "String", name = "token", value = "token", required = true),
            @ApiImplicitParam(paramType = "form", dataType = "String", name = "searchValue", value = "searchValue", required = true)})
    @ResponseBody
    @RequestMapping(value = "/findCommonParameterValuesByKey.action", method = RequestMethod.POST)
    public HttpResult<List<CommonParameterValue>> findCommonParameterValuesByKey() {
        HttpResult<List<CommonParameterValue>> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>();
            HlpUtils.copyStringValue(map, params, "key1", "key1", true, false);
            HlpUtils.copyStringValue(map, params, "key2", "key2", false, false);
            HlpUtils.copyStringValue(map, params, "key3", "key3", false, false);
            HlpUtils.copyIntegerValue(map, params, "state", "state", false, null);
            List<CommonParameterValue> list = commonParameterValueService.queryByKey(params);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询CommonParameterValue", notes = "说明")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "String", name = "token", value = "token", required = true),
            @ApiImplicitParam(paramType = "form", dataType = "String", name = "searchValue", value = "searchValue", required = true)})
    @ResponseBody
    @RequestMapping(value = "/findCommonParameters.action", method = RequestMethod.POST)
    public HttpResult<List<CommonParameterValue>> findCommonParameters() {
        HttpResult<List<CommonParameterValue>> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>();
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "searchValue", "searchValue", false, true);
            HlpUtils.copyStringValue(map, params, "skip_key1", "skip_key1", false, false);
            HlpUtils.copyStringValue(map, params, "key1", "key1", false, false);
            HlpUtils.copyStringValue(map, params, "key2", "key2", false, false);
            HlpUtils.copyStringValue(map, params, "key3", "key3", false, false);
            HlpUtils.copyStringValue(map, params, "value", "value", false, false);
            HlpUtils.copyStringValue(map, params, "displayText", "display_text", false, false);
            PageBean page = new PageBean(map);
            if (map.containsKey("nopage")) {
                page = null;
            }
            List<CommonParameterValue> list;
            if (map.containsKey("nopage")) {
                list = commonParameterValueService.findCommonParameterValues(params);
            } else {
                list = commonParameterValueService.findCommonParameterValues(params, page);
            }
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ResponseBody
    @RequestMapping(value = "/getQueryValue4ViewAttachBySid.action", method = RequestMethod.POST)
    public HttpResult<Object> getQueryValue4ViewAttachBySid() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            String paramSid = HlpUtils.getString(map, "param_sid");
            super.checkEmpty(paramSid, "请提供参数：param_sid");
            CommonParameterValue value = commonParameterValueService.queryBySid(paramSid);
            super.checkEmpty(value, "参数配置不存在");
            super.checkEmpty(value.getValue(), "请配置参数值");
            map.put("sql", value.getValue());
            List<LinkedHashMap<String, Object>> list = sqlSelectMapper.selectByParams(map);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询CommonParameterValue")
    @ResponseBody
    @RequestMapping(value = "/findCommonParameterValues.action", method = RequestMethod.POST)
    public HttpResult<Object> findParameterValues() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "searchValue", "searchValue", false, false);
            HlpUtils.copyStringValue(map, params, "key1", "key1", false, false);
            HlpUtils.copyStringValue(map, params, "key2", "key2", false, false);
            HlpUtils.copyStringValue(map, params, "key3", "key3", false, false);
            HlpUtils.copyIntegerValue(map, params, "state", "state", false, null);
            HlpUtils.copyStringValue(map, params, "selected_value", "selected_value", false, false);
            PageBean page = new PageBean(map);
            List<CommonParameterValue> list = commonParameterValueService.findCommonParameterValues(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "查询公共参数，返回对象", notes = "查询公共参数，返回对象")
    @ResponseBody
    @RequestMapping(value = "/findCommonParameterList.action", method = RequestMethod.POST)
    public HttpResult<Object> findCommonParameterList() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            String paramsList = HlpUtils.getString(map, "paramsList");
            JSONArray array = JSON.parseArray(paramsList);
            Map<String, Object> resultMap = new HashMap<>(8);
            if (!HlpUtils.isEmptyList(array)) {
                for (int i = 0; i < array.size(); i++) {
                    JSONObject object = array.getJSONObject(i);
                    String key1 = object.getString("key1");
                    String keyName = object.getString("keyName");
                    super.checkEmpty(key1, "key1不能为空");
                    super.checkEmpty(keyName, "keyName不能为空");
                    Map<String, Object> params = new HashMap<>();
                    HlpUtils.copyStringValue(object, params, "key1", "key1", true, false);
                    HlpUtils.copyStringValue(object, params, "key2", "key2", false, false);
                    HlpUtils.copyStringValue(object, params, "key3", "key3", false, false);
                    HlpUtils.copyIntegerValue(map, params, "state", "state", false, null);
                    List<CommonParameterValue> list = commonParameterValueService.queryByKey(params);
                    resultMap.put(keyName, list);
                }
            }
            hr.setData(resultMap);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
